public class solution_2269 {

    public int divisorSubstrings(int num, int k) {
        String s = Integer.toString(num); // num 十进制表示字符串
        int n = s.length();
        int res = 0;
        for (int i = 0; i <= n - k; ++i) {
            // 枚举所有长度为 k 的子串
            int tmp = Integer.parseInt(s.substring(i, i + k));
            if (tmp != 0 && num % tmp == 0) {
                ++res;
            }
        }
        return res;
    }

    public static int divisorSubStings(int num, int k) {
        // 数字长度
        String numString = Integer.toString(num);
        char[] charArray = numString.toCharArray();
        int kMatch = 0;
        int kNum = k > 1 ? k : 0;
        for (int i = 0; i < numString.length() - k + 1; i++) {
            // 当以 0开头时跳过本次循环

            String str = String.valueOf(charArray[i]);
            if (charArray[i] == '0') {
                StringBuffer buffer = new StringBuffer();
                for (int j = 1; j < k; j++) {
                    if (charArray[i + j] != '0') {
                        buffer.append(charArray[i + j]);
                    }
                }
                if (buffer.length() == 0) {
                    continue;
                }
                str = buffer.toString();
            }

            if (kNum != 0) {
                str = numString.substring(i, i + kNum);
            }
            int subNum = Integer.valueOf(str);
            if (num % subNum == 0) {
                kMatch++;
            }
        }

        return kMatch;
    }

    public static void main(String[] args) {
        System.out.println(divisorSubStings(340034, 2));
        System.out.println(divisorSubStings(240, 2));
        System.out.println("----");
        System.out.println(divisorSubStings(20050, 2));
        System.out.println(divisorSubStings(10, 2));
        System.out.println(divisorSubStings(11, 1));
        System.out.println(divisorSubStings(300003, 3));
    }
}
